package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.ProductsPageRequest;
import com.example.springboot.entity.Products;
import com.example.springboot.service.IProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin  //解决跨域错误
@RestController
@RequestMapping("/products")
//是rest风格的productscontroller
public class ProductsController {

    @Autowired
    IProductsService productsService;

    //    ProductsPageRequest传入用户名和电话信息
    @GetMapping("/page")
    public Result page(ProductsPageRequest productsPageRequest){
        //        返回数据 Result.success传入一个对象
        return Result.success(productsService.page(productsPageRequest));

    }
//    post put get delete对应增改查删四类操作
    //    新增会员
    @PostMapping("/save")
    public Result save(@RequestBody Products products){
        productsService.save(products);
        return Result.success();
    }

//    更新产品信息
    @PutMapping("/update")
    public Result update(@RequestBody Products products){
        productsService.update(products);
        return Result.success();
    }

    //    获取用户列表
    @GetMapping("/list")
    public Result list(){
        //        返回数据
        List<Products> productss=productsService.list();
        return Result.success(productss);

    }

//    根据ID更新用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Products products =productsService.getById(id);
        return Result.success(products);

    }

    //删除操作:
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        productsService.deleteById(id);
        return Result.success();
    }

}
